﻿namespace SortByDelegates
{
    using System;

    /// <summary>
    /// Sort with using delegates
    /// </summary>
    public class BubbleSort
    {
        #region Main Logic

        /// <summary>
        /// Sorts array elements
        /// </summary>
        /// <param name="array">Array of int elements</param>
        /// <param name="Criteria">Sort criteria</param>
        /// <exception cref="ArgumentNullException">If input params is null</exception>
        public static void Sort(ref int[] array, MySortCriteria Criteria)
        {
            if (array == null)
                throw new ArgumentNullException();

            if (Criteria == null)
                throw new ArgumentNullException();

            int temp;
            for (int i = 0; i < array.Length - 1; i++)
            {
                for (int j = i + 1; j < array.Length; j++)
                {
                    if (Criteria(array[i], array[j]))
                    {
                        temp = array[j];
                        array[j] = array[i];
                        array[i] = temp;
                    }
                }
            }
        }

        #endregion

        #region Sort Criteries

        /// <summary>
        /// Increase criteria
        /// </summary>
        /// <param name="a">Value 1</param>
        /// <param name="b">Value 2</param>
        /// <returns>True - is a more than b, otherwise - False</returns>
        public static bool Increase(int a, int b)
        {
            return a > b;
        }

        /// <summary>
        /// Decrease criteria
        /// </summary>
        /// <param name="a">Value 1</param>
        /// <param name="b">Value 2</param>
        /// <returns>True - is a less than b, otherwise - False</returns>
        public static bool Decrease(int a, int b)
        {
            return a < b;
        }

        #endregion

        #region My delegate

        /// <summary>
        /// Sort criteria declaretion
        /// </summary>
        public delegate bool MySortCriteria(int a, int b);

        #endregion
    }
}
